package com.its.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.its.pojo.BusInsurancePolicy;
import com.its.mapper.BusInsurancePolicyMapper;
import com.its.service.BusInsurancePolicyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.its.web.param.BusInsurancePolicyControllerListParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朱厚强
 */
@Service
public class BusInsurancePolicyServiceImpl extends ServiceImpl<BusInsurancePolicyMapper, BusInsurancePolicy> implements BusInsurancePolicyService {
    @Autowired
    private BusInsurancePolicyMapper mapper;
    @Override
    public PageInfo<BusInsurancePolicy> selectList(BusInsurancePolicyControllerListParam busInsurancePolicyControllerListParam) {
        // 开始分页
        PageHelper.startPage(busInsurancePolicyControllerListParam.getPageNum(), busInsurancePolicyControllerListParam.getPageSize());
        // 用lambda 表达式的方式生成
        LambdaQueryWrapper<BusInsurancePolicy> queryWrapper = new LambdaQueryWrapper<>();

        // 保单号 需要数据库中的字段与实体类的属性进行相匹配
        if (StringUtils.isNotBlank(busInsurancePolicyControllerListParam.getPolicyNumber())){
            queryWrapper.eq(BusInsurancePolicy::getPolicyNumber, busInsurancePolicyControllerListParam.getPolicyNumber());
        }
        // 投保人
        if (StringUtils.isNotBlank(busInsurancePolicyControllerListParam.getInsuredName())){
             queryWrapper.eq(BusInsurancePolicy::getInsuredName, busInsurancePolicyControllerListParam.getInsuredName());
        }
        // 身份证号
        if (StringUtils.isNotBlank(busInsurancePolicyControllerListParam.getInsuredId())){
            queryWrapper.eq(BusInsurancePolicy::getInsuredId, busInsurancePolicyControllerListParam.getInsuredId());
        }
        // 投保时间
        if (busInsurancePolicyControllerListParam.getPaymentDate() != null){
            queryWrapper.eq(BusInsurancePolicy::getPaymentDate, busInsurancePolicyControllerListParam.getPaymentDate());
        }
        // 投保时间
        // 假设 getPolicyStatus 返回的是整数类型
        if (busInsurancePolicyControllerListParam.getPolicyStatus() != null) {
            // 进行其他处理，比如构建查询条件
            queryWrapper.eq(BusInsurancePolicy::getPolicyStatus, busInsurancePolicyControllerListParam.getPolicyStatus());
        }
        // 只查询在保的保单
        queryWrapper.eq(BusInsurancePolicy::getPolicyStatus,1);
        List<BusInsurancePolicy> list = mapper.selectList(queryWrapper); // 条件查询后的集合
        return new PageInfo<>(list);
    }

    /**
     * 根据被保人姓名以及被保人身份证号获取报案信息
     * @return
     */
    @Override
    public BusInsurancePolicy getByInsuredNameAndCardID(String name, String cardID) {
        return mapper.getByInsuredNameAndCardID(name,cardID);
    }

    @Override
    public List<BusInsurancePolicy> getByinsuredId(String insuredId) {
        // 用lambda 表达式的方式生成
        LambdaQueryWrapper<BusInsurancePolicy> queryWrapper = new LambdaQueryWrapper<>();
        // 身份证号
        if (StringUtils.isNotBlank(insuredId)){
            queryWrapper.eq(BusInsurancePolicy::getInsuredId,insuredId);
        }
        // 只查询在保的保单
        queryWrapper.eq(BusInsurancePolicy::getPolicyStatus,1);
        List<BusInsurancePolicy> list = mapper.selectList(queryWrapper);
        return list;
    }
}
